package com.lichao.FinkDataProcess.compare;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author: li chao
 * @Desc:
 * @create: 2024-11-15 21:14
 **/
public class StringCompareNode extends CompareNode {

    public StringCompareNode(String fieldName, String operateType, Object value) {
        super(fieldName, operateType, value);
    }

    @Override
    public boolean compare(JSONObject json) throws Exception {
        Object jsonValue = json.get(this.getFieldName());
        if (jsonValue == null) {
            return false;
        }

        switch (this.getOperateType()) {
            case "=":
                return jsonValue.toString().equals(this.getValue().toString());
            case "!=":
                return !jsonValue.toString().equals(this.getValue().toString());
            case "in":
                JSONArray values = (JSONArray) this.getValue();
                return values.contains(jsonValue);
            default:
        }

        return false;
    }
}
